package com.ewzj.bigdata.mvp.model

import com.ewzj.bigdata.app.config.netmodel.CommonService
import com.ewzj.bigdata.app.utils.Apputils
import com.ewzj.bigdata.mvp.contract.LoginContract
import com.ewzj.bigdata.mvp.model.reqbean.ReqLoginbean
import com.ewzj.bigdata.mvp.model.reqbean.ReqThirdbean
import com.ewzj.bigdata.mvp.model.reqbean.Reqcodebean
import com.ewzj.bigdata.mvp.model.rspbean.BaseData
import com.ewzj.bigdata.mvp.model.rspbean.TokenBean
import com.google.gson.Gson
import com.jess.arms.integration.IRepositoryManager
import com.jess.arms.mvp.BaseModel
import io.reactivex.Observable
import okhttp3.RequestBody
import javax.inject.Inject

/**
 * author:zhangjing
 * 作用:
 * return:
 */
class LoginModel @Inject constructor(val mGson:Gson,repositoryManager: IRepositoryManager) :BaseModel(repositoryManager),LoginContract.Model{
    val service=repositoryManager.obtainRetrofitService(CommonService::class.java)
    override fun login(username: String, password: String): Observable<BaseData<TokenBean>> {
        val reqLoginbean = ReqLoginbean()
        reqLoginbean.setMobile(username)
        reqLoginbean.setCode(password)
        val logindata = mGson.toJson(reqLoginbean)
        return service.Login(Apputils.Utils.json2Requestbody(logindata))
    }

    override fun loginByThird(token: String, open_id: String, origin: String, form: String): Observable<BaseData<TokenBean>> {
        val reqThirdbean = ReqThirdbean()
        reqThirdbean.setToken(token)
        reqThirdbean.setOpen_id(open_id)
        reqThirdbean.setOrigin(origin)
        reqThirdbean.setForm(form)
        val json = mGson.toJson(reqThirdbean)
        return service.LoginbyThird(Apputils.Utils.json2Requestbody(json))
    }

    override fun sendSms(phone: String): Observable<BaseData<Any>> {
        val codebean = Reqcodebean()
        codebean.setMobile(phone)
        val jsonString = mGson.toJson(codebean)
        val body = Apputils.Utils.json2Requestbody(jsonString)
        return service.Sms(body)
    }

}